package com.xuecheng.media.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.CreateUploadVideoRequest;
import com.aliyuncs.vod.model.v20170321.CreateUploadVideoResponse;
import com.aliyuncs.vod.model.v20170321.RefreshUploadVideoRequest;
import com.aliyuncs.vod.model.v20170321.RefreshUploadVideoResponse;
import com.xuecheng.api.media.AliyunVodApi;
import com.xuecheng.api.media.model.aliyun.VodUploadRequest;
import com.xuecheng.api.media.model.aliyun.VodUploadToken;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

/**
 * <p></p>
 *
 * @Description:
 */
@Slf4j
@RestController
public class AliyunVodController implements AliyunVodApi {

//    String accessKeyId = "LTAI5tKPuEAuAdVs2m2HywwL";
//    String accessKeySecret = "2YT0a6PWsjwNX9lSyN6qEYvUPANNUH";
//    String regionId = "cn-shanghai";  // 点播服务接入区域
    @Value("${aliyun.accessKeyId}")
    private String accessKeyId;
    @Value("${aliyun.accessKeySecret}")
    private String accessKeySecret;
    @Value("${aliyun.region}")
    private String regionId;
    /**
     * 初始化代码
     * @param accessKeyId
     * @param accessKeySecret
     * @return
     * @throws
     */
    public static DefaultAcsClient initVodClient(String regionId,String accessKeyId, String accessKeySecret) {
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
        DefaultAcsClient client = new DefaultAcsClient(profile);
        return client;
    }
    public static RefreshUploadVideoResponse refreshUploadVideo(DefaultAcsClient client,String videoId) throws Exception {
        RefreshUploadVideoRequest request = new RefreshUploadVideoRequest();
        //音/视频ID
        request.setVideoId(videoId);
        return client.getAcsResponse(request);
    }

    @PostMapping("media/vod-token")
    public VodUploadToken generateUploadToken(@RequestBody VodUploadRequest uploadRequest) {
        CreateUploadVideoRequest request = new CreateUploadVideoRequest();
        request.setTitle(uploadRequest.getTitle());
        request.setFileName(uploadRequest.getFileName());
        //2.获得客户端对象
        DefaultAcsClient client = initVodClient(regionId,accessKeyId, accessKeySecret);
        VodUploadToken vodUploadToken = new VodUploadToken();
        // PS: 只要和本项目中微服务之外的内容要产生交互，必须要进行try-catch处理
        //   交互方：aliyun（第三方服务）
        //   原因：网络（不稳定因素）
        try {
            //1.创建请求对象
            // 3.获得响应对象
            CreateUploadVideoResponse response = client.getAcsResponse(request);
            System.out.print("VideoId = " + response.getVideoId() + "\n");
            System.out.print("UploadAddress = " + response.getUploadAddress() + "\n");
            System.out.print("UploadAuth = " + response.getUploadAuth() + "\n");
//            vodUploadToken.setVideoId(response.getVideoId());
//            vodUploadToken.setRequestId(response.getRequestId());
//            vodUploadToken.setUploadAddress(response.getUploadAddress());
//            vodUploadToken.setUploadAuth(response.getUploadAuth());
            BeanUtils.copyProperties(response,vodUploadToken);
        } catch (Exception e) {
            // printStackTrace：将程序中的所有调用关系的栈信息进行打印-日志（日志文件-IO）
            // 用户大量请求
            // e.printStackTrace();  //丢工作
            System.out.print("ErrorMessage = " + e.getLocalizedMessage());
            // 异常信息的说明
            // 1.只将错误信息记录
            // 2.将错误信息隐藏并返回友好提示
        }
        return vodUploadToken;
    }

    @GetMapping("media/refresh-vod-token/{videoId}")
    public VodUploadToken refreshUploadToken(@PathVariable String videoId) {

        DefaultAcsClient client = initVodClient(regionId,accessKeyId, accessKeySecret);
        VodUploadToken vodUploadToken = new VodUploadToken();
        // PS: 只要和本项目中微服务之外的内容要产生交互，必须要进行try-catch处理
        //   交互方：aliyun（第三方服务）
        //   原因：网络（不稳定因素）
        try {
            RefreshUploadVideoResponse response1 = refreshUploadVideo(client, videoId);
            vodUploadToken.setVideoId(response1.getVideoId());
            vodUploadToken.setRequestId(response1.getRequestId());
            vodUploadToken.setUploadAddress(response1.getUploadAddress());
            vodUploadToken.setUploadAuth(response1.getUploadAuth());
        } catch (Exception e) {
            // printStackTrace：将程序中的所有调用关系的栈信息进行打印-日志（日志文件-IO）
            // 用户大量请求
            // e.printStackTrace();  //丢工作
            System.out.print("ErrorMessage = " + e.getLocalizedMessage());
            // 异常信息的说明
            // 1.只将错误信息记录
            // 2.将错误信息隐藏并返回友好提示
        }
        return vodUploadToken;
    }
}